package com.dfqy.controller;

import com.dfqy.pojo.User;
import com.dfqy.service.UserService;
import com.dfqy.util.*;
import com.dfqy.vo.Page;
import com.dfqy.vo.Result;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author : atyushu
 * Package : com.dfqy.controller
 * ClassName : UserController
 * Create : 2023/7/31 - 13:53
 * Version : v1.0
 * Description : The class of controller for user.
 */
@Validated
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        simpleDateFormat.setLenient(false);
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }

    @GetMapping("userPage")
    public Page<User> page(@RequestParam(name = "page", defaultValue = "1") @Range(min = 1, message = "页码过小") int page,
                           @RequestParam(name = "limit", defaultValue = "10") @Range(min = 1, message = "条数过小") int limit,
                           @RequestParam(name = "uname", required = false) String uname,
                           @RequestParam(name = "phone", required = false) String phone,
                           @RequestParam(name = "regtimeStart", required = false) Date regtimeStart,
                           @RequestParam(name = "regTimeEnd", required = false) Date regTimeEnd,
                           @RequestParam(name = "qq", required = false) String qq,
                           @RequestParam(name = "weibo", required = false) String weibo
    ) {
        return userService.page(page, limit, uname, phone, regtimeStart, regTimeEnd, qq, weibo);
    }

    @GetMapping("deleteUser/{id}")
    public Result delete(@PathVariable long id) {
        int delete = userService.delete(id);
        return delete > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("batchDeleteUser/{id}")
    public Result batchDelete(@PathVariable String id) {
        String[] idStr = id.split(",");
        long[] ids = new long[idStr.length];
        for (int i = 0; i < idStr.length; i++) {
            ids[i] = Long.parseLong(idStr[i]);
        }
        int delete = userService.delete(ids);
        return delete > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("banUser/{id}")
    public Result ban(@PathVariable long id) {
        int ban = userService.ban(id);
        return ban > 0 ? Result.success("封禁成功") : Result.error("封禁失败");
    }

    @GetMapping("restoreUser/{id}")
    public Result restore(@PathVariable long id) {
        int restore = userService.restore(id);
        return restore > 0 ? Result.success("恢复成功") : Result.error("恢复失败");
    }

    @GetMapping("reloadPass/{id}/{pass}")
    public Result reloadPass(@PathVariable("id") long id,@PathVariable("pass") String pass) {
        int reloadPass = userService.reloadPass(id, Md5Utils.hash(pass));
        return reloadPass > 0 ? Result.success("重置成功") : Result.error("重置失败");
    }

    @GetMapping("getUserById/{id}")
    public Result getById(@PathVariable("id") long id) {
        User user = userService.getById(id);
        return user == null ? Result.error("无数据") : Result.success(user);
    }
}